package com.xym.manage.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.abel533.entity.Example;
import com.github.abel533.mapper.Mapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xym.manage.mapper.ItemMapper;
import com.xym.manage.pojo.Item;
import com.xym.manage.pojo.ItemDesc;

@Service
public class ItemService extends BaseService<Item>{
	
	@Autowired
	private ApiService apiService;
	@Autowired
	private ItemMapper itemMapper;
	
	@Autowired
	private ItemCatService itemCatService;
	
	@Autowired
	private ItemDescService itemDescService;

	@Override
	public Mapper<Item> getMapper() {
		return this.itemMapper;
	}

	public Boolean saveItemAndItemDesc(Item item,String desc){
		item.setStatus(1);
		item.setId(null);
		
		String cname =itemCatService.queryById(item.getCid()).getName();
		item.setCname(cname);
		Integer i = super.save(item);
		
		ItemDesc itemDesc = new ItemDesc();
		itemDesc.setItemId(item.getId());
		itemDesc.setItemDesc(desc);
		Integer j = itemDescService.save(itemDesc);
		if(i>0&&j>0){
			return true;
		}
		return false;
	}

	/**
	 * 更新商品
	 * @param item
	 * @param desc
	 * @return
	 */
	public Boolean updata(Item item, String desc) {
		
		String cname =itemCatService.queryById(item.getCid()).getName();
		item.setCname(cname);
		Integer i = super.updateSelective(item);
		
		ItemDesc itemDesc = new ItemDesc();
		itemDesc.setItemId(item.getId());
		itemDesc.setItemDesc(desc);
		Integer j = itemDescService.updateSelective(itemDesc);
		if(i>0&&j>0){
			String uri = "http://www.xym.com/cache/item/"+item.getId()+".html";
			try {
				this.apiService.doPost(uri);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return true;
		}
		return false;
	}

	/**
	 * 下架商品
	 * @param ids
	 * @return
	 */
	public Boolean instock(Long[] ids) {
		
		Integer a =itemMapper.updateInstock(ids);
		if(a>0){
			return true;
		}
		return false;
	}

	/**
	 * 上架商品
	 * @param ids
	 * @return
	 */
	public Boolean reshelf(Long[] ids) {
		Integer a =itemMapper.updateReshelf(ids);
		if(a>0){
			return true;
		}
		return false;
	}

	/**
	 * 逻辑删除商品
	 * @param ids
	 * @return
	 */
	public Boolean deleteSetStatus(Long[] ids) {
		Integer a =itemMapper.deleteSetStatus(ids);
		if(a>0){
			return true;
		}
		return false;
	}

	public List<Item> queryPageList(Integer pageNum, Integer rows) {
			
	        List<Item> list = itemMapper.queryPageListByStatus((pageNum-1)*rows,rows);
	        return list;
	}

	public Integer queryCountByStatus() {
		Example example = new Example(Item.class);
		example.createCriteria().andBetween("status", 1, 2);
		int count = itemMapper.selectCountByExample(example);
		return count;
	}

	
}
